package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.OrderMapper;
import com.bwie.pojo.Group;
import com.bwie.pojo.Order;
import com.bwie.service.GroupService;
import com.bwie.mapper.GroupMapper;
import com.bwie.utils.R;
import com.bwie.vo.GroupVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 陶仕豪
 * @description 针对表【tb_group(用户参团表)】的数据库操作Service实现
 * @createDate 2025-07-27 19:06:34
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
        implements GroupService {
    @Autowired
    GroupMapper groupMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    HttpServletRequest request;
    @Override
    public R getGroupList(GroupVo groupVo) {
        Page<Group> groupPage = new Page<>(groupVo.getPageNum(), groupVo.getPageSize());
        Page<Group> page = groupMapper.getGroupList(groupPage, groupVo);
        return R.OK(page);
    }

    @Override
    public R getOrderListById() {
        String token = request.getHeader("token");
        if (token == null) {
            return R.ERROR(500, "请先登录");
        }
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = body.get("userId", Integer.class);
       List<Order> list= orderMapper.getOrderListById(userId);

        return R.OK(list);
    }

    @Override
    public R deleteOrder(Order order) {
        Order tbOrder = orderMapper.selectById(order.getOrderId());
        if(tbOrder==null){
            return R.ERROR(500,"订单不存在");
        }
        orderMapper.deleteById(order.getOrderId());
        return R.OK();
    }
}




